﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Object;

namespace Sweepids.Models.Dao
{
    public class ZipDao : Dao
    {
        public void Create(Zip zip)
        {
            try
            {
                var myCommand = new OracleCommand("INSERT INTO SWEEP_ZIP " +
                                                                  "(ZIP, STATE, CITY) " +
                                                                  "VALUES " +
                                                                  "(:ZIP, :STATE, :CITY)", myConnection);
                RunCommand(myCommand, zip.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("ZipDao::Create(Zip): " + e.Message);
                throw;
            }
        }

        public Zip Read(string zip)
        {
            try
            {
                var result = new Zip { ZipId = zip };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_ZIP " +
                                                  "WHERE ZIP=:ZIP", myConnection);
                var myReader = RunCommand(myCommand, result.OracleParameters, true);
                while (myReader.Read())
                {
                    result = new Zip(myReader);
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("ZipDao::Read(string): " + e.Message);
                throw;
            }
        }

        public void Update(Zip zip)
        {
            try
            {
                var myCommand = new OracleCommand("UPDATE SWEEP_ZIP SET " +
                                                  "ZIP=:ZIP, " +
                                                  "STATE=:STATE, " +
                                                  "CITY=:CITY " +
                                                  "WHERE ZIP=:ZIP", myConnection);
                RunCommand(myCommand, zip.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("ZipDao::Update(Zip): " + e.Message);
                throw;
            }
        }

        public void Delete(Zip zip)
        {
            try
            {
                var myCommand = new OracleCommand("DELETE FROM SWEEP_ZIP " +
                                                  "WHERE ZIP=:ZIP", myConnection);
                RunCommand(myCommand, zip.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("ZipDao::Delete(Zip): " + e.Message);
                throw;
            }
        }
    }
}